package com.lxw.nonghe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxw.nonghe.annotation.RequiresPermission;
import com.lxw.nonghe.common.Result;
import com.lxw.nonghe.entity.Role;
import com.lxw.nonghe.service.RoleService;
import com.lxw.nonghe.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/role")
@RequiresPermission("role:manage")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private LogUtils logUtils;

    @PostMapping("/create")
    public Result<Boolean> addRole(@RequestBody Role role) {
        try {
            // 检查角色名称是否已存在
            if (roleService.getRoleByRoleName(role.getRoleName()) != null) {
                logUtils.recordLog("角色管理", "新增角色", "失败", "角色名称已存在:" + role.getRoleName());
                return Result.error("该角色名称已存在，请重新编写角色名称");
            }

            boolean result = roleService.save(role);
            if (result) {
                logUtils.recordLog("角色管理", "新增角色", "成功",
                        "角色ID:" + role.getRoleId() + ", 名称:" + role.getRoleName());
                return Result.success("角色添加成功", true);
            } else {
                logUtils.recordLog("角色管理", "新增角色", "失败", "保存数据库失败");
                return Result.error("角色添加失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("角色管理", "新增角色", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/delete/{id}")
    public Result<Boolean> deleteRole(@PathVariable Integer id) {
        try {
            Role role = roleService.getById(id);
            boolean result = roleService.removeById(id);

            if (result) {
                logUtils.recordLog("角色管理", "删除角色", "成功",
                        "角色ID:" + id + ", 名称:" + (role != null ? role.getRoleName() : "未知"));
                return Result.success("角色删除成功", true);
            } else {
                logUtils.recordLog("角色管理", "删除角色", "失败", "角色ID:" + id + "不存在");
                return Result.error("角色删除失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("角色管理", "删除角色", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/update")
    public Result<Boolean> updateRole(@RequestBody Role role) {
        try {
            // 检查角色名称是否已被其他角色使用
            Role existingRole = roleService.getRoleByRoleName(role.getRoleName());
            if (existingRole != null && !existingRole.getRoleId().equals(role.getRoleId())) {
                logUtils.recordLog("角色管理", "更新角色", "失败", "角色名称冲突:" + role.getRoleName());
                return Result.error("该角色名称已存在，请重新编写角色名称");
            }

            boolean result = roleService.updateById(role);
            if (result) {
                logUtils.recordLog("角色管理", "更新角色", "成功",
                        "角色ID:" + role.getRoleId());
                return Result.success("角色更新成功", true);
            } else {
                logUtils.recordLog("角色管理", "更新角色", "失败",
                        "角色ID:" + role.getRoleId() + "不存在");
                return Result.error("角色更新失败");
            }
        } catch (Exception e) {
            logUtils.recordLog("角色管理", "更新角色", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @GetMapping("/get/{id}")
    public Result<Role> getRoleById(@PathVariable Integer id) {
        try {
            Role role = roleService.getById(id);
            if (role != null) {
                /*logUtils.recordLog("角色管理", "查询角色详情", "成功", "角色ID:" + id);*/
                return Result.success("角色查询成功", role);
            } else {
                logUtils.recordLog("角色管理", "查询角色详情", "失败", "角色ID:" + id + "不存在");
                return Result.error("未找到该角色");
            }
        } catch (Exception e) {
            logUtils.recordLog("角色管理", "查询角色详情", "失败", e.getMessage());
            return Result.error("查询异常: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result<Page<Role>> getRolePage(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String roleName) {
        try {
            Page<Role> rolePage = roleService.getRolePage(current, size, roleName);
            /*logUtils.recordLog("角色管理", "分页查询角色", "成功",
                    "页码:" + current + ", 大小:" + size + ", 角色名称:" + roleName);*/
            return Result.success("角色分页查询成功", rolePage);
        } catch (Exception e) {
            logUtils.recordLog("角色管理", "分页查询角色", "失败", e.getMessage());
            return Result.error("分页查询异常: " + e.getMessage());
        }
    }

    @PostMapping("/query")
    public Result<Page<Role>> queryRole(
            @RequestBody Role queryRole,
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size) {
        try {
            Page<Role> rolePage = roleService.queryRoles(queryRole, current, size);
            /*logUtils.recordLog("角色管理", "条件查询角色", "成功",
                    "条件:" + queryRole.toString() + ", 页码:" + current + ", 大小:" + size);*/
            return Result.success("角色条件查询成功", rolePage);
        } catch (Exception e) {
            logUtils.recordLog("角色管理", "条件查询角色", "失败", e.getMessage());
            return Result.error("条件查询异常: " + e.getMessage());
        }
    }
}
